import {createStore} from 'vuex'
import { userApi } from '@/http/userApi'
import { USER_TOKEN } from '@/constants/local-storage-key-const'
export default createStore({
	state:{
		statusBarHeight:10,
		titleBarWidth:350,
		titleBarHeight:40,
		menuButtonHeight:32,
		inviteCode:'',
		loginState:false,
		token:'',
		balance:0,
		id:'',
		openid:'',
		nickName:'',
		avatar:'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/app/tem-car-img/49927.jpg',
		phone:'',
		online_status:1,
		gender:1,
		homeSettingFlag:false,
		unions_name:'',
		real_name:'',
		id_card: '',
		assess_type:'',
		created_at:''
	},
	mutations:{
		setInviteCode(state,code){
			state.inviteCode = code
		},
		setUserInfo(state,userInfo){
			state.loginState = true
			state.id = userInfo.id
			state.openid = userInfo.openid
			state.nickName = userInfo.name
			state.avatar = userInfo.avatar
			state.phone = userInfo.phone
			state.online_status =userInfo.online_status
			state.balance = userInfo.balance
			state.gender = userInfo.gender
			state.unions_name = userInfo.unions_name
			state.real_name = userInfo.real_name
			state.id_card = userInfo.id_card
			state.assess_type = userInfo.type
			state.created_at = userInfo.created_at
		},
		logOut(state){
			state.loginState = false
			state.id = ''
			state.openid = ''
			state.nickName = '游客'
			state.avatar = 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/app/tem-car-img/49927.jpg'
			state.phone = ''
			uni.clearStorageSync(USER_TOKEN)
		},
		setStatusBarHeight(state,statusBarHeight){
			state.statusBarHeight = statusBarHeight
		},
		setWxHeader(state,wxHeader){
			state.statusBarHeight = wxHeader.statusBarHeight
			state.menuButtonHeight = wxHeader.menuButtonHeight
			state.titleBarWidth = wxHeader.titleBarWidth
			state.titleBarHeight = wxHeader.titleBarHeight
		},
		setHomeSettingFlag (state,flag) {
			state.homeSettingFlag = flag
		}
	},
	actions:{
		// getLocalUser({commit}){
		// 	uni.getStorage({
		// 		key:USER_INFO,
		// 		success: (res) => {
		// 			if(typeof res.data !== "object") return
		// 			let d = JSON.parse(JSON.stringify(res.data))
		// 			if(d == null) return
		// 			commit('setUserInfo', res.data)
		// 		}
		// 	})
		// },
		refreshUser({commit,state}){
			if(state.id != ''){
				userApi.getUserInfo(state.id).then((res)=>{
					commit('setUserInfo',res)
				}).catch((e)=>{
					console.log('e',e)
				})
				
			}
		}
	}
})